package com.smartleanx.module.hrm.service.employee;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.smartleanx.module.hrm.controller.admin.employee.edu.EmployeeEducationExperienceSaveReqVO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeEducationExperienceDO;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeEducationExperienceMapper;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 员工教育经历 Service 实现类
 *
 * @author tengcj
 */
@Service
@Validated
public class EmployeeEducationExperienceServiceImpl implements EmployeeEducationExperienceService {

    @Resource
    private EmployeeEducationExperienceMapper employeeEducationExperienceMapper;
    @Resource
    private EmployeeService employeeService;

    @Override
    public Long createEmployeeEducationExperience(EmployeeEducationExperienceSaveReqVO createReqVO) {
        // 插入
        EmployeeEducationExperienceDO employeeEducationExperience = BeanUtils.toBean(createReqVO, EmployeeEducationExperienceDO.class);
        // 校验时间有效性
        validateEducationExperienceTime(createReqVO);
        // 校验员工是否存在
        EmployeeDO employeeDO = employeeService.getEmployeeById(createReqVO.getEmployeeId());
        if (employeeDO == null) {
            throw exception(EMPLOYEE_NOT_EXISTS);
        }
        List<EmployeeEducationExperienceDO> experienceList = getEmployeeEducationExperienceList(createReqVO.getEmployeeId());
        employeeEducationExperience.setSort(CollUtil.isEmpty(experienceList) ? 0 : experienceList.get(0).getSort() + 1);
        employeeEducationExperienceMapper.insert(employeeEducationExperience);
        // 返回
        return employeeEducationExperience.getId();
    }

    @Override
    public void updateEmployeeEducationExperience(EmployeeEducationExperienceSaveReqVO updateReqVO) {
        // 校验存在
        validateEmployeeEducationExperienceExists(updateReqVO.getId());
        // 校验时间有效性
        validateEducationExperienceTime(updateReqVO);
        // 校验员工是否存在
        EmployeeDO employeeDO = employeeService.getEmployeeById(updateReqVO.getEmployeeId());
        if (employeeDO == null) {
            throw exception(EMPLOYEE_NOT_EXISTS);
        }
        // 更新
        EmployeeEducationExperienceDO updateObj = BeanUtils.toBean(updateReqVO, EmployeeEducationExperienceDO.class);
        employeeEducationExperienceMapper.updateById(updateObj);
    }

    public void validateEducationExperienceTime(EmployeeEducationExperienceSaveReqVO vo) {
        if (Objects.nonNull(vo.getAdmissionTime()) && Objects.nonNull(vo.getGraduationTime())) {
            if (vo.getAdmissionTime().compareTo(vo.getGraduationTime()) > 0) {
                throw exception(ADMISSION_TIME_UNLATER_GRADUATION_TIME);
            }
        }
    }

    @Override
    public void deleteEmployeeEducationExperience(Long id) {
        // 校验存在
        validateEmployeeEducationExperienceExists(id);
        // 删除
        employeeEducationExperienceMapper.deleteById(id);
    }

    private void validateEmployeeEducationExperienceExists(Long id) {
        if (employeeEducationExperienceMapper.selectById(id) == null) {
            throw exception(EMPLOYEE_EDUCATION_EXPERIENCE_NOT_EXISTS);
        }
    }

    @Override
    public EmployeeEducationExperienceDO getEmployeeEducationExperience(Long id) {
        return employeeEducationExperienceMapper.selectById(id);
    }

    @Override
    public List<EmployeeEducationExperienceDO> getEmployeeEducationExperienceList(Long employeeId) {
        return employeeEducationExperienceMapper.getEmployeeEducationExperienceList(employeeId);
    }

    @Override
    public void deleteByEmployeeId(Long employeeId) {
        employeeEducationExperienceMapper.delete(new LambdaQueryWrapper<EmployeeEducationExperienceDO>(){{
            eq(EmployeeEducationExperienceDO::getEmployeeId,employeeId);
        }});
    }
}
